package cn.gov.pbc.dream.pojo.bo;

import cn.gov.pbc.dream.common.BankFinancingMessageConst;
import cn.gov.pbc.dream.util.DateTimeUtil;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.Date;

@Data
@ToString
@Slf4j
public class BankFinancingMessageInfoBo {

    private BankFinancingMessageConst.Sign messageType;

    private String originFilename;

    private String orgCode;

    private Date reportDate;

    private int sequence;

    private File file;

    private BankFinancingMessageInfoBo() {

    }

    public static BankFinancingMessageInfoBo from(String filename, File file) {
        if (filename == null) {
            return null;
        }
        if (filename.length() < 32) {
            return null;
        }
        BankFinancingMessageInfoBo bo = new BankFinancingMessageInfoBo();

        BankFinancingMessageConst.Sign sign = BankFinancingMessageConst.Sign.codeOf(filename.substring(0, 3));
        if (sign == null) {
            return null;
        }
        String orgCode = filename.substring(3, 17);
        Date date = DateTimeUtil.strToDate(filename.substring(17, 25));
        if (date == null) {
            return null;
        }
        int sequence;
        try {
            sequence = Integer.parseInt(filename.substring(25, 28));
        } catch (Exception e){
            log.error(e.getStackTrace().toString());
            return null;
        }
        bo.setMessageType(sign);
        bo.setOrgCode(orgCode);
        bo.setReportDate(date);
        bo.setSequence(sequence);
        bo.setFile(file);
        bo.setOriginFilename(filename);
        return bo;
    }
}
